<?php
    namespace app\common\controller;
    use think\Controller;
    class Base  extends  Controller{
        public function _initialize() {
//             header("Access-Control-Allow-Origin:*");
//             header("application/x-www-form-urlencoded; charset=UTF-8");
//             header("Access-Control-Allow-Headers: Origin, X-Requested- With, Content-Type, Accept");
            $inputJSON = file_get_contents('php://input');
            //print_r($inputJSON);exit;
            $input = json_decode($inputJSON, TRUE);
            $this->data = $input;
        }

        /**
         * 发送HTTP请求方法，目前只支持CURL发送请求
         * @param  string $url    请求URL
         * @param  array  $params 请求参数
         * @param  string $method 请求方法GET/POST
         * @return array  $data   响应数据
         */
        public function api_http($url, $params, $method = 'POST', $header = array(), $multi = false){

            // return 123;exit;
            try{
                $opts = array(
                    CURLOPT_TIMEOUT        => 30,
                    CURLOPT_RETURNTRANSFER => 1,
                    CURLOPT_SSL_VERIFYPEER => false,
                    CURLOPT_SSL_VERIFYHOST => false,
                    CURLOPT_HTTPHEADER     => $header,
                    CURLOPT_COOKIESESSION     => false,
                //HTTP_USER_AGENT => true
                );

                /* 根据请求类型设置特定参数 */
                switch(strtoupper($method)){
                    case 'GET':
                        $opts[CURLOPT_URL] = $url . '?' . http_build_query($params);
                        break;
                    case 'POST':
                        //判断是否传输文件
                        //$params = $multi ? $params : http_build_query($params);
                        $opts[CURLOPT_URL] = $url;
                        $opts[CURLOPT_POST] = 1;
                        $opts[CURLOPT_POSTFIELDS] = $params;
                        break;
                    default:
                        throw new Exception('不支持的请求方式！');
                }

                /* 初始化并执行curl请求 */
                $ch = curl_init();
                curl_setopt_array($ch, $opts);

                //curl_setopt($ch, CURLOPT_HEADER, true);
                curl_setopt ($ch, CURLOPT_COOKIE ,"value:".cookie('test'));
                $data  = curl_exec($ch);
                $error = curl_error($ch);
                curl_close($ch);
                if($error) throw new Exception('请求发生错误：' . $error);
                return  $data;
            }catch (\Exception $e){
                throw $e;
            }
        }

    }